<?php
class Lib2k_Parsing_cUrl {
    
    private $_html;
    private $_headers;
    private $_user_agent;

    public function __construct(){
		$this->_headers [] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
		$this->_headers [] = 'Accept-Language: uk,ru;q=0.8,en-us;q=0.5,en;q=0.3';
		$this->_headers [] = 'Connection: Keep-Alive';
	//	$this->_headers [] = 'Accept-Encoding: gzip,deflate';
        $this->_headers [] = 'Accept-Encoding: identity';
		$this->_headers [] = 'Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7';
		$this->_headers [] = 'Keep-Alive: 300';      
        
        $this->_user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; uk; rv:1.9.1.3) Gecko/20090824';  
    }
    
    // get request
    public function get($url){
        $ch = curl_init();        
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$this->_headers); 
        // Set so curl_exec returns the result instead of outputting it.
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt ( $ch, CURLOPT_USERAGENT, $this->_user_agent);
        // Get the response and close the channel.
        $response = curl_exec($ch);  
        $this->_html = $response;      
    }
    
    public function getHtml(){
        return $this->_html;
    }


    //Alternative Image Saving Using cURL seeing as allow_url_fopen is disabled - bummer
    function save_image($img,$destDir){
        
        $fullpath = $destDir . "/" . basename($img);
        
        $ch = curl_init ($img);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
        $rawdata=curl_exec($ch);
        curl_close ($ch);
        if(file_exists($fullpath)){
            unlink($fullpath);
        }
        $fp = fopen($fullpath,'x');
        fwrite($fp, $rawdata);
        fclose($fp);
        return $fullpath;        
    }

}